﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;
using MxWeiXinPF.WeiXinComm;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.Groups;
using Senparc.Weixin.MP.AdvancedAPIs.User;

namespace MxWeiXinPF.Web.admin.crm
{
    public partial class user_list : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string status = "";
        protected int group_id;
        protected string keywords = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            ChkAdminLevel("fensimgr", MXEnums.ActionEnum.View.ToString()); //检查权限
            this.group_id = MXRequest.GetQueryInt("group_id");
            status = MXRequest.GetQueryString("status");
            this.keywords = MXRequest.GetQueryString("keywords");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ValidAccessToken(); 
                GroupBind(); //绑定类别

                Model.wx_userweixin weixin = GetWeiXinCode();
                RptBind("u.wid=" + weixin.id + " " + CombSqlTxt(this.status,this.group_id, this.keywords), "groupid desc,id desc");
            }
        } 


        #region 绑定组别=================================
        private void GroupBind()
        {
            BLL.wx_crm_group gBll = new BLL.wx_crm_group();
            Model.wx_userweixin weixin = GetWeiXinCode();
            IList<Model.wx_crm_group> grouplist = gBll.GetModelList("wid=" + weixin.id);

            this.ddlGroupId.Items.Clear();
            this.ddlGroupId.Items.Add(new ListItem("所有分组", "-1"));
            if (grouplist == null)
            {
                return;
            }
            foreach (Model.wx_crm_group g in grouplist)
            {
                string iName = g.name + "(" + g.count.Value.ToString() + ")";
                this.ddlGroupId.Items.Add(new ListItem(iName, g.groupid.ToString()));
            }
        }
        #endregion

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = MXRequest.GetQueryInt("page", 1);
            if (this.group_id > 0)
            {
                this.ddlGroupId.SelectedValue = this.group_id.ToString();
            }
            if (this.status.Trim().Length > 0)
            {
                this.ddlstatus.SelectedValue = this.status.ToString();
            }
           
            this.txtKeywords.Text = this.keywords;


            BLL.wx_crm_users bll = new BLL.wx_crm_users();
            DataSet ds = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    dr = ds.Tables[0].Rows[i];
                    dr["subscribe_time"] = MyCommFun.GetTime(dr["subscribe_time"].ToString()).ToString();
                }
            }
            this.rptList.DataSource = ds;
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("user_list.aspx", "status={0}&group_id={1}&keywords={2}&page={3}",
            this.status.ToString(),this.group_id.ToString(), this.keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string status,int _group_id, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            
            if (_group_id > 0)
            {
                strTemp.Append(" and u.groupId=" + _group_id);
            }
            if (status.Trim().Length > 0)
            {
                strTemp.Append(" and u.uStatus="+status); 
            }
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (u.nickname like '%" + _keywords + "%' or u.city like '%" + _keywords + "%' or g.[name] like '%" + _keywords + "%' or t.tag like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回用户每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("user_list_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        #region 返回用户性别=============================


        /// <summary>
        /// 获得用户的性别
        /// </summary>
        /// <param name="sexNum"></param>
        /// <returns></returns>
        protected string GetUserSex(int sexNum)
        {
            string result = string.Empty;
            switch (sexNum)
            {
                case 0:
                    result = "未知";
                    break;
                case 1:
                    result = "男";
                    break;
                case 2:
                    result = "女";
                    break;

            }
            return result;
        }

        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("user_list.aspx", "status={0}&group_id={1}&keywords={2}",
               status.ToString(), this.group_id.ToString(), txtKeywords.Text));
        }

        //筛选类别
        protected void ddlGroupId_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("user_list.aspx", "status={0}&group_id={1}&keywords={2}",
                 status.ToString(), ddlGroupId.SelectedValue, this.keywords));
        }

        //筛选状态
        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("user_list.aspx", "status={0}&group_id={1}&keywords={2}",
               ddlstatus.SelectedValue, this.group_id.ToString(), this.keywords));
        }


        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("user_list_page_size", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("user_list.aspx", "group_id={0}&keywords={1}",
                this.group_id.ToString(), this.keywords));
        }


      

        /// <summary>
        /// 验证accesstoken
        /// </summary>
        /// <returns></returns>
        public bool ValidAccessToken()
        {
            string err = "";
            Model.wx_userweixin weixin = GetWeiXinCode();

            string accessToken = WeiXinCRMComm.getAccessToken(weixin.id, out err);
            if (err != "")
            {
                lblInfo.Text = "该功能仅限于认证过的服务号，其他的都无法使用！AccessToken获取失败，请检查AppId和AppSecret填写是否正确！错误信息如下：" + err;
                lblInfo.ForeColor = System.Drawing.Color.Red;
                //btnSyn.Enabled = false;
                btnSyn.Style.Add("display", "none");
                hidErr.Value = lblInfo.Text;
                MessageBox.ResponseScript(this, " $(\"#btn_anniu\").hide();");
                return false;
            }
            else
            {
                hidErr.Value = "";
                //获取上次更新的时间
                BLL.wx_crm_setting setBll = new BLL.wx_crm_setting();
                Model.wx_crm_setting setting = setBll.GetModelByWid(weixin.id);
                if (setting == null || setting.personSynDate == null)
                {
                    lblInfo.Text = "请点击按钮更新分组信息";
                }
                else
                {
                    lblInfo.Text = "上次更新时间为：" + setting.personSynDate.Value;
                }
                return true;
            }

        }



        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("fensimgr", MXEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.wx_crm_users bll = new BLL.wx_crm_users();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(MXEnums.ActionEnum.Delete.ToString(), "删除粉丝信息" + sucCount + "条，失败" + errorCount + "条"); //记录日志

            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("user_list.aspx", "keywords={0}", this.keywords));
        }


        /// <summary>
        /// 清空所有用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteAll_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("fensimgr", MXEnums.ActionEnum.Delete.ToString()); //检查权限
          
            BLL.wx_crm_users bll = new BLL.wx_crm_users();
            Model.wx_userweixin weixin = GetWeiXinCode();
           bool ret= bll.DeleteByWid(weixin.id);
           if (ret)
           {
               AddAdminLog(MXEnums.ActionEnum.Delete.ToString(), "删除所有粉丝信息成功"); //记录日志

               JscriptMsg("删除所有粉丝信息成功", Utils.CombUrlTxt("user_list.aspx", "keywords={0}", this.keywords));
           }
           else
           {
               AddAdminLog(MXEnums.ActionEnum.Delete.ToString(), "删除所有粉丝信息失败"); //记录日志

               JscriptMsg("删除所有粉丝信息失败", Utils.CombUrlTxt("user_list.aspx", "keywords={0}", this.keywords));
           }
        }
 


    }
}